﻿using AnesSystem.Controls;
using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace AnesSystem.ViewModels
{
    public class ConfigDepartmentsCtrlViewModel : BaseViewModel<ConfigDepartmentsCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";

        private bool editDept = true;
        private DispatcherTimer timer;

        private ObservableCollection<ConfigDepartmentsModel> _displayConfigDepartmentsModels = new ObservableCollection<ConfigDepartmentsModel>();

        #endregion

        #region Constructor

        public ConfigDepartmentsCtrlViewModel()
        {
            InitalTimer();
        }

        #endregion

        #region Properties
        public string SeachValue
        {
            get
            {
                return _seachValue;
            }
            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigDepartmentsModels");

            }
        }
        public ObservableCollection<ConfigDepartmentsModel> DisplayConfigDepartmentsModels
        {
            get
            {
                _displayConfigDepartmentsModels.Clear();
                List<ConfigDepartmentsModel> lst = ConfigDapartmentDataGridViewModel.Instance.ConfigDepartmentsModels.Where(O =>
                O.Name.Contains(_seachValue) || O.Code.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigDepartmentsModels.Add(item);
                }
                return _displayConfigDepartmentsModels;
            }

            set
            {
                ConfigDapartmentDataGridViewModel.Instance.ConfigDepartmentsModels = value;
                RaisePropertyChanged("DisplayConfigDepartmentsModels");
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDapartmentService.Instance.Update(sender as ConfigDepartmentsModel);
        }
        private void AddConfigDepartment(ConfigDepartmentsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigDapartmentService.Instance.Update(obj);
                ConfigDapartmentDataGridViewModel.Instance.ConfigDepartmentsModels.Add(obj);
            }
            else ConfigDapartmentService.Instance.Insert(obj);


        }

        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 20);
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            ConfigDapartmentDataGridViewModel.Instance.Refresh();
            RaisePropertyChanged("DisplayConfigDepartmentsModels");
            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
            timer.Stop();
        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigDepartmentsModel(ConfigDepartmentsModel configDepartmentsModel)
        {
            if (configDepartmentsModel != null)
            {
                ConfigDapartmentDataGridViewModel.Instance.ConfigDepartmentsModels.Remove(configDepartmentsModel);
                RaisePropertyChanged("DisplayConfigDepartmentsModels");
                ConfigDapartmentService.Instance.DeleteById(configDepartmentsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDepartmentsModel model = new ConfigDepartmentsModel();
            DisplayConfigDepartmentsModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        public void getDeptFromHis()
        {
            Task.Factory.StartNew(delegate
            {
                UdpMangager.Instance.SendUdpServerMsg(ServiceSign.DEPART_ADD, new UdpModel());
                timer.Start();//执行查询方法的线程  
            });
            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
        }
        #endregion

        #region EventHandlers

        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDepartment(sender as ConfigDepartmentsModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
